//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>
#include <ulinux/error.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "objs.h"
#include "wire/wl_shm.h"
#include "srv/registry.h"
//------------------------------------------------------------------------------

static k_u32 id;
#define FMTS_N_MAX 32
static k_u fmts_n=0;
static k_u32 fmts[FMTS_N_MAX];

static void ev_format(k_u32 fmt)
{
  if(fmts_n>=FMTS_N_MAX){
    PERRC("FATAL:too many shm formats\n");
    sysc(exit_group,1,-1);
  }
  fmts[fmts_n++]=fmt;
  //DEBUG
  PERR("shm:received format %u\n",fmt);
}

struct wl_shm_listener shm_listener={
  ev_format
};

void shm_bind(void)
{
  k_u shm;
  //we work with the first shm object advertized in the registry
  for(shm=0;shm<REGISTRY_N_MAX;++shm) if(registry[shm].itf==WL_SHM) break;
  if(shm==REGISTRY_N_MAX){
    PERRC("FATAL:unable to find a shm singleton in registry\n");
    sysc(exit_group,1,-1);
  }

  id=registry_bind(&registry[shm],&shm_listener);
}

k_u32 shm_create_pool(k_i fd,k_u32 sz,void *listener)
{
  k_u32 shm_pool_id=objs_add(WL_SHM_POOL,listener);
  if(shm_pool_id==OBJS_MAX){
    PERRC("FATAL:unable to install the shm pool object\n");
    sysc(exit_group,1,-1);
  }

  wl_shm_req_create_pool(id,shm_pool_id,fd,sz);
  return shm_pool_id;
}
